package mo.offer_special;

public class L105 {

    private static int[] x = {0, 0, 1, -1};
    private static int[] y = {1, -1, 0, 0};

    public int maxAreaOfIsland(int[][] grid) {
        int ans = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                ans = Math.max(ans, dfs(grid, i, j));
            }
        }
        return ans;
    }

    private int dfs(int[][] grid, int curI, int curJ) {
        if (curI < 0 || curJ < 0 || curI >= grid.length || curJ >= grid[0].length || grid[curI][curJ] != 1) {
            return 0;
        }
        grid[curI][curJ] = 0;
        int ans = 1;
        for (int i = 0; i < 4; i++) {
            ans += dfs(grid, curI+x[i], curJ+y[i]);
        }
        return ans;
    }

}
